﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using BLToolkit.Data;
using BLToolkit.Data.Linq;

namespace DAL
{
    public class HoaDonRepository : IHoaDonRepository
    {
        private readonly DbManager db = new DbManager("QuanLyBilliards");
        public HoaDon GetByMaHoaDon(string maHD)
        {
            return db.GetTable<HoaDon>().SingleOrDefault(hd => hd.MaHD == maHD);
        }

        public IQueryable<HoaDon> GetAll()
        {
            return db.GetTable<HoaDon>();
        }

        // lay theo hoa don nam
        public IQueryable<HoaDon> TimHoaDonTheoNam(String nam)
        {
            return db.GetTable<HoaDon>().Where(hd => hd.NgayLap.Substring(hd.NgayLap.Length - 4, 4) == nam);

        }
        public IQueryable<HoaDon> TimHoaDonTheoThang(string thang)
        {
            return db.GetTable<HoaDon>().Where(hd => hd.NgayLap.Substring(hd.NgayLap.Length - 7, 7) == thang);
        }
        //public HoaDon Get
        public IQueryable<HoaDon> TimHoaDonTheoBan(string tenban)
        {
            return db.GetTable<HoaDon>().Where(tb => tb.TenBan.Contains(tenban));
        }
        public IQueryable<HoaDon> TimHoaDonTheoLoai(bool loai)
        {
            return db.GetTable<HoaDon>().Where(hd => hd.LoaiHD == loai);
        }
        public IQueryable<HoaDon> TimHoaDonTheoNguoiLap(string nl)
        {
            return db.GetTable<HoaDon>().Where(hd=>hd.NguoiLap.Contains(nl));
        }
        public IQueryable<HoaDon> TimHoaDonTheoNgay(string ngay)
        {
            return db.GetTable<HoaDon>().Where(hd => hd.NgayLap.CompareTo(ngay)==0);
        }
        public void AddNew(HoaDon hoaDon)
        {
            db.Insert(hoaDon);
        }

        public void Update(HoaDon hoaDon)
        {
            throw new NotImplementedException();
        }
        public bool ThemHoaDon(HoaDon hdt)
        {
            return db.Insert(hdt) > 0;
        }
        public bool Delete(HoaDon hoaDon)
        {
           return  db.Delete(hoaDon)>0;
        }
        public IQueryable<HoaDon> GetByNgay(string ngay)
        {
            return db.GetTable<HoaDon>().Where(hd => hd.NgayLap == ngay);
        }
        public IQueryable<HoaDon> GetbyQuy(string quy)
        {
            throw new NotImplementedException();
        }

        public IQueryable<HoaDon> getbyNam(string nam)
        {
            throw new NotImplementedException();
        }


        public IQueryable<HoaDon> GetByThang(string thang)
        {
            return db.GetTable<HoaDon>().Where(hd => hd.NgayLap.Substring(hd.NgayLap.Length - 7, 7) == thang);
        }


        public IQueryable<HoaDon> TimHoaDonTheoTTTinhTien(bool tinhtien)
        {
            return db.GetTable<HoaDon>().Where(hd => hd.TinhTien == tinhtien);
        }


        public void Xoa(HoaDon a)
        {
            db.Delete(a);

        }
    }
}
